//--------------------------------------------------- // Purpose: Program to calculate magic numbers // Author: John Gauch //--------------------------------------------------- #include using namespace std; // Function to calculate magic number int Magic(const int Number) { // Get digits and calculate sum int Digit1, Digit2, Digit3, Digit4, Digit5; Digit1 = (Number / 10000); Digit2 = (Number % 10000) / 1000; Digit3 = (Number % 1000) / 100; Digit4 = (Number % 100) / 10; Digit5 = (Number % 10); return (Digit1 + Digit2 + Digit3 + Digit4 + Digit5); } // Main body of program int main() { // Get user input int Input, Output; cout << "Enter number in [0..99999] range: "; cin >> Input; // Do error checking if ((Input < 0) || (Input > 99999)) cout << "Number must be in [0..99999]" << endl; // Calculate magic number and print results else { cout << "Input = " << Input << endl; Output = Magic(Input); cout << "Output = " << Output << endl; while (Output > 9) { Output = Magic(Output); cout << "Output = " << Output << endl; } } return 0; }